package com.littcore.saas.rest.assistant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.littcore.common.Utility;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.exception.NotLoginException;
import com.littcore.saap.common.vo.IUserInfo;
import com.littcore.saap.core.common.SaapConstants;
import com.littcore.saap.core.common.SaapConstants.UserGroupType;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.system.biz.ITenantBizService;
import com.littcore.saap.system.biz.IUserBizService;
import com.littcore.saap.system.po.UserInfo;
import com.littcore.saap.system.service.IRoleService;
import com.littcore.saap.system.service.ITenantMemberService;
import com.littcore.saap.system.service.IUserGroupMemberService;
import com.littcore.saap.system.service.IUserGroupService;
import com.littcore.saap.system.service.IUserInfoService;
import com.littcore.saap.system.vo.UserGroupTreeNode;
import com.littcore.saap.system.vo.UserInfoVo;
import com.littcore.util.BeanCopier;
import com.littcore.web.util.WebUtils;

@Api(name = "租户成员接口", description = "租户成员管理")
@Controller
public class TenantmemberServiceController {

    @Resource
    private ITenantMemberService tenantMemberService;
    @Resource
    private ITenantBizService tenantBizService;
    @Resource
    private IUserInfoService userInfoService;
    @Resource
    private IUserBizService userBizService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IUserGroupService userGroupService;
    @Resource
    private IUserGroupMemberService userGroupMemberService;
    

    @ApiMethod(description = "获取所有用户")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public List listAll(WebRequest request, ModelMap modelMap) throws NotLoginException
    { 
  
      //get params from request
      Map<String, Object> params = WebUtils.getParametersStartingWith(request, SaapConstants.DEFAULT_SEARCH_PREFIX);
        
      String searchField = Utility.trimNull(params.get("searchField"));
      Object searchValue = params.get("searchValue");
          
      //package the params
      PageParam pageParam = WebUtils.getPageParam(request);
      pageParam.addCond("tenantId", LoginUtils.getTenantId());
      pageParam.addCond(searchField, searchValue);  
      pageParam.addCond("isDeleted", false);  
      //Get page result
      IPageList pageList = tenantMemberService.listPage(pageParam);   
      return pageList.getRsList();
    }
    
    @ApiMethod(description = "获取组织结构")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public  UserGroupTreeNode getGroupTree() throws NotLoginException
    { 
       //Get page result
      UserGroupTreeNode userGroupTree = userGroupService.findTree(LoginUtils.getTenantId());
      setTreeMember(userGroupTree);
      setNotInGroupMember(userGroupTree);
      return userGroupTree;
    
    } 
    
    private void setTreeMember(UserGroupTreeNode userGroupTree) {
        if (userGroupTree.getType() != UserGroupType.GROUP) {
            List<UserGroupTreeNode> subs = userGroupTree.getSubList();
            if (subs != null && subs.size() > 0) {
                for (UserGroupTreeNode sub : subs) {
                    setTreeMember(sub);
                }
            }
        } else {
            List<Map<String, Object>> memberList = userGroupMemberService.listWithUserInfoByGroup(userGroupTree.getId());
            userGroupTree.setMemberList(memberList);
        }
    }
    
    private void setNotInGroupMember(UserGroupTreeNode userGroupTree){
        UserGroupTreeNode otherNode=new UserGroupTreeNode();
        otherNode.setId(-2);
        otherNode.setLeaf(true);
        otherNode.setParentId(userGroupTree.getId());
        otherNode.setType(UserGroupType.GROUP);
        otherNode.setName("未分组");
        List<UserInfo> users=userGroupMemberService.listNotGroupUsers(LoginUtils.getTenantId());
        List<Map<String, Object>> memberList=new ArrayList<Map<String,Object>>();
        if(users!=null&&users.size()>=1){
            for(UserInfo user:users){
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("userInfo", user);
                memberList.add(map);
            }
        }
        otherNode.setMemberList(memberList);
        userGroupTree.getSubList().add(otherNode);
    }
    
    
    
    @ApiMethod(description = "获取用户组成员")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public List getGroupMembers(@ApiQueryParam(name = "userGroupId")@RequestParam Integer userGroupId)
    {         
      List<Map<String, Object>> memberList = userGroupMemberService.listWithUserInfoByGroup(userGroupId);
      return memberList;
     
    }
    
    @ApiMethod(description = "获取用户详情")
    @ApiResponseObject
    @ResponseBody
    @RequestMapping 
    public IUserInfo getUserInfo(@ApiQueryParam(name = "userId")@RequestParam Integer userId)
    {         
      UserInfo userInfo=userInfoService.load(userId);
      UserInfoVo userInfoVo=BeanCopier.copy(userInfo, UserInfoVo.class);
      return userInfoVo;
     
    }
    
    
    
    
 
    

}
